﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace winSpriteCreator
{
  public partial class Form1 : Form
  {
    private Bitmap _bmp;

    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Bitmap bmp = CreateBitMap();
      AddLinesToBitmap(ref bmp);
      pbResult.Image = bmp;
      _bmp = bmp;
    }

    private Bitmap CreateBitMap()
    {
      Bitmap bmp = new Bitmap((int)(nudColumns.Value*nudWidth.Value) + (int)((nudColumns.Value-1)), (int)(nudRows.Value*nudHeight.Value)+(int)(nudRows.Value-1), PixelFormat.Format32bppArgb);
     
      return bmp;
    }

    private void AddLinesToBitmap(ref Bitmap bmp)
    {
      Graphics g = Graphics.FromImage(bmp);
      g.FillRectangle(new SolidBrush(pbBackGround.BackColor), new Rectangle(0, 0, bmp.Size.Width, bmp.Size.Height));
      
      // Vertical Lines
      for(int index=1; index<nudColumns.Value; index++ )
      {
        g.DrawLines(new Pen(pbLines.BackColor), new Point[]
                                                      {
                                                        new Point((int)(index*(nudWidth.Value+1))+1,0),
                                                        new Point((int)(index*(nudWidth.Value+1))+1,bmp.Size.Height) 
                                                      } );
      }

      // Horzontal Lines
      for (int index = 1; index < nudColumns.Value; index++)
      {
        g.DrawLines(new Pen(pbLines.BackColor), new Point[]
                                                      {
                                                        new Point(0, (int)(index*(nudHeight.Value+1))+1),
                                                        new Point(bmp.Size.Width,(int)(index*(nudHeight.Value+1))+1) 
                                                      });
      }
    }

    private void AddRowOfSprites( ref Bitmap bmp, string[] sprites, int row)
    {
      Graphics g = Graphics.FromImage(bmp);

      int width = Convert.ToInt32(nudWidth.Value);
      int height = Convert.ToInt32(nudHeight.Value);
      int square = 0;

      foreach( string fileName in sprites )
      {
        Image tempBmp = Bitmap.FromFile(fileName);

        g.DrawImage(tempBmp, (float)(square * width) - (width / 2 + tempBmp.Size.Width / 2) + ( square+1), (float)((row+1) * height) - (height / 2 + tempBmp.Size.Height / 2) + row);

        square++;
      }

    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
      colorDialog1.AllowFullOpen = true;
      colorDialog1.ShowDialog(this);
      pbBackGround.BackColor = colorDialog1.Color;
    }

    private void pbLines_Click(object sender, EventArgs e)
    {
      colorDialog1.AllowFullOpen = true;
      colorDialog1.ShowDialog(this);
      pbLines.BackColor = colorDialog1.Color;
    }

    private void button2_Click(object sender, EventArgs e)
    {
      saveFileDialog1.ShowDialog(this);
      _bmp.Save(saveFileDialog1.FileName);
    }

    private void button3_Click(object sender, EventArgs e)
    {

   
      MessageBox.Show("Please select all files that will be inserted on a row.");

      OpenFileDialog openFile = new OpenFileDialog();
      openFile.Multiselect = true;
      openFile.ShowDialog(this);


      AddRowOfSprites(ref _bmp, openFile.FileNames, (int)numInsertRow.Value);

      pbResult.Refresh();
    }


  }
}
